package cn.com.wxUtil;


import java.net.MalformedURLException;
import java.net.URL;

import cn.com.util.HttpUtil;
import cn.com.wxUtil.wxConstant.*;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.qiniu.util.Json;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static cn.com.wxUtil.wxConstant.*;

/**
 * Created with IntelliJ IDEA.
 * User: yue
 * Date: 2018-03-30
 * Time: 19:59
 * Description:
 */
public class WXUtil {

    public static Logger logger= LoggerFactory.getLogger(WXUtil.class);

    public static String access_token="";

    public static void getAccessToken(){
        URL url = null;
        try {
            url = new URL(
                    AT_URL + "?grant_type=" + AT_GRANT_TYPE +
                            "&appid=" + APPID + "&secret=" + SECRET);
        } catch (MalformedURLException e) {
            logger.error("构建access_token请求URL失败,{}",url.toString());
        }
        String response = HttpUtil.doGet(url);
        JSONObject wxresponse= JSON.parseObject(response);
        if (wxresponse.containsKey("errcode")){
            logger.error("请求access_token失败,errorcode:{},msg:{}",
                    wxresponse.get("errcode"),wxresponse.get("errmsg"));
            return;
        }
        access_token=(String) wxresponse.get("access_token");
        logger.info("成功获取access_token:{}",access_token);
    }

    public static wxUserAccessToken getUserAccessToken(String code) {
        URL url = null;
        try {
            url = new URL(USER_AT_URL + "?appid=" + APPID + "&secret=" + SECRET +
                    "&code=" + code + "&grant_type=" + USER_AT_GRANT_TYPE);
            String response = HttpUtil.doGet(url);
            JSONObject wxresponse=JSON.parseObject(response);
            if (wxresponse.containsKey("errcode")){
                logger.error("请求user_access_token失败,errorcode:{},msg:{}",
                        wxresponse.get("errcode"),wxresponse.get("errmsg"));
                return null;
            }
            return JSON.parseObject(response,wxUserAccessToken.class);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String getUserInfo(String accessToken, String openid) {
        URL url = null;
        try {
            url = new URL(USER_INFO_URL + "?access_token=" + accessToken +
                    "&openid=" + openid + "&lang=zh_CN"
            );
            String response = HttpUtil.doGet(url);
            JSONObject wxresponse=JSON.parseObject(response);
            if (wxresponse.containsKey("errcode")){
                logger.error("请求access_token失败,errorcode:{},msg:{}",
                        wxresponse.get("errcode"),wxresponse.get("errmsg"));
                return null;
            }
            return response;
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        return "";
    }

    public static JSONObject getUnionCode(String code) {
        URL url = null;
        try {
            url = new URL(UNIONID_URL + "?appid=" + APPID + "&secret=" + SECRET +
                    "&js_code=" + code + "&grant_type=" + USER_AT_GRANT_TYPE);
            String response = HttpUtil.doGet(url);
            JSONObject wxresponse=JSON.parseObject(response);
            if (wxresponse.containsKey("errcode")){
                logger.error("请求获取unionid失败,errorcode:{},msg:{}",
                        wxresponse.get("errcode"),wxresponse.get("errmsg"));
                return null;
            }
            return wxresponse;
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        return null;
    }

}
